package com.zzyl.nursing.service.impl;

import java.util.List;

import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.dto.NursingProjectPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-21
 */
@Service
public class NursingPlanServiceImpl implements INursingPlanService 
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;



    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        return nursingPlanMapper.selectNursingPlanById(id);
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlan nursingPlan)
    {
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        return nursingPlanMapper.insertNursingPlan(nursingPlan);
    }

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    /**
     * 修改护理计划
     * 
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNursingPlan(NursingPlanDto dto)
    {
        if(dto.getProjectPlans()!=null && dto.getProjectPlans().size()>0){
            //删除当前计划所有项目
            nursingProjectPlanMapper.deletePlanId(dto.getId());

            //修改护理项目计划关系
//        return nursingPlanMapper.updateNursingPlan(nursingPlan);
            nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(),dto.getId());
        }
        //修改护理计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto,nursingPlan);
        nursingPlan.setUpdateTime(DateUtils.getNowDate());
        return nursingPlanMapper.updateNursingPlan(nursingPlan);
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long id)
    {
        nursingPlanMapper.deletePlanById(id);
        return nursingPlanMapper.deleteNursingPlanById(id);
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        return nursingPlanMapper.deleteNursingPlanById(id);
    }


    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto dto) {
        //获取列表名称信息
//        List<NursingPlan> nursingPlans = nursingPlanMapper.selectNursingPlanList(new NursingPlan());
        //判断name属性是否重复
//        for (NursingPlan nursingPlan : nursingPlans) {
//            if (nursingPlan.getPlanName().equals(dto.getPlanName())) {
//                //抛出异常
//                throw new RuntimeException("名称重复");
//            }
//        }
        NursingPlan  nursingPlans_flag= nursingPlanMapper.selectName(dto.getPlanName());
        List<NursingProjectPlanDto> listDtos = dto.getProjectPlans();
        if (nursingPlans_flag!=null){
                throw new BaseException("名称重复");//BaseException注解: 自定义异常
        }

        if(listDtos.size()>0&&listDtos.get(0).getProjectId()==""){
            throw new BaseException("请选择护理项目");
        }
        //保存护理计划
        //属性拷贝
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto,nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);
        //获取id
        Long id=nursingPlan.getId();

        //批量保存护理项目计划关系

        Integer count = nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(), id);
        return count;
//    return count == 0 ? 0 : 1;
    }

    @Override
    public List<NursingPlanVo> selectAll() {
        return nursingPlanMapper.selectAll();
    }
}
